package kr.hkit.powerjava.chapter24.work2.service;

import java.util.InputMismatchException;
import java.util.Scanner;

import kr.hkit.powerjava.chapter24.work2.dao.InterfaceDao;

public abstract class ServiceItems<T> {
	protected InterfaceDao<T> dao;
	protected Scanner input;
	
	public ServiceItems(InterfaceDao<T> dao, Scanner input) {
		this.dao = dao;
		this.input = input;
		init();
		prnMenu();
	}
	
	public abstract T inputItemInfo(boolean gubun);
	
	public abstract void prnSubMenu();
	
	protected void init() {
		dao.loadFile();
	}
	
	protected void close() {
		dao.saveFile();
	}
	
	public void prnMenu() {
		T item = null;
		int res;
		try {
		do {
			System.out.print("1.입력\n2.삭제\n3.수정\n4.검색\n5.리스트보기\n6.종료\n");
			
			res = input.nextInt();
			switch(res) {
				case 1:
					//학생정보 입력
					item = inputItemInfo(true); //학번, 성명, 국어, 영어, 수학
					dao.insertItem(item);
					break;
				case 2:
					// 리스트를 보여주고 삭제할 번호를 입력 받는다.
					dao.prnItemList(null);
					System.out.println("삭제할 정보를 입력하시오.");
					item = inputItemInfo(false);
					dao.deleteItem(item);
					break;
				case 3:
					// 리스트를 보여주고 수정할 학생정보를 입력받음
					dao.prnItemList(null);
					item = inputItemInfo(true);
					dao.updateItem(item);
					
					break;
				case 4:
					//검색
					item = inputItemInfo(false);
					item = dao.searchItem(item);
					System.out.println(item);
					
					break;
				case 5:
					prnSubMenu();
					break;
				case 6:
					dao.saveFile();
					input.close();
					System.exit(0);
					break;
				default:
					close();
					res = 7;
			}
		}
		while(res <= 6);
		}catch(InputMismatchException e) {
			System.out.println("잘못 입력하셨습니다.");
		}
	}
	
}
